package com.gjintel.framework.log.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gjintel.framework.log.dao.IMessageDao;
import com.gjintel.framework.log.model.TSysWarningLog;
import com.gjintel.framework.user.model.User;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

@Repository("messageDao")
@SuppressWarnings("unchecked")
public class MessageDaoImpl  extends HibernateGenericDAO implements IMessageDao{

	@Override
	public List<TSysWarningLog> findAllByIds(TSysWarningLog warningLog, Page page, Sort sort) {
		StringBuffer hql =new StringBuffer("from TSysWarningLog wl where 1=1  and readFlag=0");
		List<Object> params = new ArrayList<Object>();
		if(warningLog!=null){
			if(warningLog.getReaderId()!=null
					&&!StringUtil.isEmpty(warningLog.getReaderId())){
				hql.append(" and wl.readerId=?");
				params.add(warningLog.getReaderId());
			}
			if(warningLog.getReadFlag()!=null){
				hql.append(" and wl.readFlag=?");
				params.add(warningLog.getReadFlag());
			}
		}
		hql.append(" order by wl.createTime desc");
		return this.findAsPage(hql.toString(), page, sort, "wl", false, params.toArray());
	}

	@Override
	public List<TSysWarningLog> getMessageList(TSysWarningLog warningLog, Page page,User u,java.sql.Date startTime, java.sql.Date endTime,Integer flag) {
		List<Object> params = new ArrayList<Object>();
		
		StringBuffer hql =new StringBuffer("from TSysWarningLog wl where wl.readerId=?");
		params.add(u.getId());
		if(startTime != null){
			hql.append("and wl.createTime >= ? ");
			params.add(startTime);
		}	
	    if(endTime !=null){
	    	hql.append("and wl.createTime <= ? ");
	    	params.add(endTime);
	    }
	    if(flag!=null){
	    	hql.append("and wl.readFlag = ? ");
	    	params.add(flag);
	    }
	     hql.append("order by wl.createTime desc"); 	
		return this.findAsPage(hql.toString(), page,null, "wl", true,params.toArray());
	}

	@Override
	public int getCountByReaderId(String readerId) {
		String hql="from TSysWarningLog wl where wl.readerId=?";
		return this.getHQLCount(hql, readerId);
	}
	
}
